#pragma once

#include "fxcc/core/graph/common/pch.h"

namespace fxcc
{
    namespace graph
    {
        namespace common
        {
            struct FXCC_API     SkinnedMeshData
            {

                std::vector<glm::ivec4> bones1s;
                std::vector<glm::vec4> weights1s;
                std::vector<glm::ivec4> bones2s;
                std::vector<glm::vec4> weights2s;
                void Resize(int numVertices);
                bool AddBoneWeight(int vertexId, int boneId, float weightValue);

                struct  Header
                {
                    uint32_t version;        
                    size_t numBones1s;
                    size_t numWeights1s;
                    size_t numBones2s;
                    size_t numWeights2s;
                };
                void ResizeHeader(const Header& header);

                void Clear()
                {
                    bones1s = {};
                    weights1s = {};
                    bones2s = {};
                    weights2s = {};
                }

                static const uint32_t CURRENT_VERSION = 1;

                Header GetHeader() const
                {
                    Header header;
                    header.version = CURRENT_VERSION;
                    header.numBones1s = bones1s.size();
                    header.numWeights1s = weights1s.size();
                    header.numBones2s = bones2s.size();
                    header.numWeights2s = weights2s.size();
                    return header;
                }


                void Write(std::ofstream& ofs);

                bool Read(std::ifstream& ifs);

                bool WriteToFile(const std::string& path)
                {
                    fxcc::Base::CreateParentDirs(path);

                    std::ofstream ofs(path, std::ios::binary);
                    if (!ofs.is_open())
                    {
                        std::cerr << "Failed to open file " << path << " for writing.\n";
                        return false;
                    }

                    Write(ofs);

                    ofs.flush();
                    ofs.close();
                    return true;
                }

                bool ReadFromFile(const std::string& path)
                {
                    std::ifstream ifs(path, std::ios::binary);
                    if (!ifs.is_open())
                    {
                        std::cerr << "Failed to open file " << path << " for reading.\n";
                        return false;
                    }

                    bool result = Read(ifs);

                    ifs.close();
                    return result;
                }
            };
        };
    };
}
